# -*- coding: utf-8 -*-
from __future__ import unicode_literals
# import sys
# reload(sys)
# sys.setdefaultencoding('utf-8')


import os,tempfile,zipfile
import time
import json
import xlwt
import shutil
from django.shortcuts import render,render_to_response
from django.http import HttpResponse,StreamingHttpResponse
from ETL.word2txt import Word2Txt
from ETL.mainHelper import qq,address,email,mobilphone,telphone,url,weichat,companyName,otherHandle
from ETL.myUtil import makeDir,ZipUtilities,readTxtContent,otherTempHandle,replacing,handleChengdui,flagChengDui
import logging
from demo.config import wordManyInfoDir,wordDir,itemdir,customInfoDir,maxSize,customInfoDirTemp
from wsgiref.util import FileWrapper
from ETL.unitSlice import FileOperation
from django.template import RequestContext
# from demo.config import customInfoDir


logger = logging.getLogger("django")
def index(request):
    logger.warning("qing qiu page......")
    return render_to_response('ceshi/uploadfile.html')


def uploadhandle(request):
    if request.method == "POST":    # 请求方法为POST时，进行处理
        myFile =request.FILES.get("wordfile", None)    # 获取上传的文件，如果没有文件，则默认为None

        if not myFile:
            return HttpResponse("请选择上传文件！")

        if myFile.size > 1024*1024*maxSize:
            return HttpResponse("上传的文件大小不能超过1M")
        name=myFile.name.split(".")   #文件名+后缀
        filenameMd5=Word2Txt.getMd5(name[0]+str(time.time()).decode('utf-8'))  #文件名md5
        filename=filenameMd5+u"."+name[1]   #文件名md5+后缀
        # ----------------------------------------------------------------------------
        destination = open(os.path.join(wordDir,filename),'wb+')    # 打开特定的文件进行二进制的写操作
        for chunk in myFile.chunks():      # 分块写入文件
            destination.write(chunk)
        destination.close()
        # ----------------------------------------------------------------------------
        fileDir=wordManyInfoDir        #用来表示创建上传文件对应的文件夹存放的父目录 F:\\target\\fileDir
        makeDir(fileDir,filenameMd5)   #一个word文件对应一个txt文档

        #公司信息分块处理
        segmentCompanyInfo(filename,filenameMd5)
        # 开始调用信息处理方法
        tmp=buildCompanyInfoResult(filenameMd5)
        return render_to_response('ceshi/result.html',{'jieguo2':tmp,'filenameMd5':filenameMd5})

def segmentCompanyInfo(fileName,parentDirMd5):
    w2t=Word2Txt()
    w2t.word2CompanyInfoSegment(wordDir+"\\",fileName,parentDirMd5)

# 页面显示，每一类一个信息
def buildCompanyInfoResult(wordFileMd5):
    wordDir=wordManyInfoDir
    targetDir=wordDir+"\\"+wordFileMd5
    manyCompanyDir=os.listdir(targetDir)

    result=[]
    for eachDir in manyCompanyDir:
        eachCompanyDir=targetDir+"\\"+eachDir
        fileList=os.listdir(eachCompanyDir)      #表示一个数据块中有多少个联系方式单元
        # if len(fileList)<5:        #每一个单元模块都会有fullInfo.txt，联系单元种类阈值设置为3
        #     continue
        qqL=qq(eachCompanyDir,fileList,eachDir)
        emailL=email(eachCompanyDir,fileList,eachDir)
        # 地址仅仅识别一个
        addressL=address(eachCompanyDir,fileList,eachDir)
        mobilphoneL=mobilphone(eachCompanyDir,fileList,eachDir)
        telphoneL=[]
        Temp=telphone(eachCompanyDir,fileList,eachDir)
        telphoneL+=Temp[0]
        mobilphoneL+=Temp[1]

        urlL=url(eachCompanyDir,fileList,eachDir)
        weichatL=weichat(eachCompanyDir,fileList,eachDir)
        companyNameL=companyName(eachCompanyDir,fileList,eachDir)
        # --------------------------------------------------------
        otherRes=otherHandle(eachCompanyDir,fileList,eachDir)

        emailL+=otherRes['email']
        addressL+=otherRes['address']
        urlL+=otherRes['url']
        mobilphoneL+=otherRes['mobilphone']
        telphoneL+=otherRes['telphone']

        res={}
        res['qq']=showOne(qqL)
        res['email']=showOne(emailL)
        res['address']=showOne(addressL)
        res['mobilphone']=showOne(mobilphoneL)
        res['telphone']=showOne(telphoneL)
        res['url']=showOne(urlL)
        res['weichat']=showOne(weichatL)
        res['companyName']=showOne(companyNameL)
        result.append(res)

    print "--------------------------"
    print result
    print "--------------------------"

    return result

def buildCompanyInfoResult1(wordFileMd5):
    wordDir=wordManyInfoDir
    targetDir=wordDir+"\\"+wordFileMd5
    manyCompanyDir=os.listdir(targetDir)

    result=[]
    for eachDir in manyCompanyDir:
        if len(eachDir) <10:
            continue
        eachCompanyDir=targetDir+"\\"+eachDir
        fileList=os.listdir(eachCompanyDir)

        qqL=qq(eachCompanyDir,fileList,eachDir)
        emailL=email(eachCompanyDir,fileList,eachDir)
        addressL=address(eachCompanyDir,fileList,eachDir)
        mobilphoneL=mobilphone(eachCompanyDir,fileList,eachDir)
        telphoneL=[]
        Temp=telphone(eachCompanyDir,fileList,eachDir)
        telphoneL+=Temp[0]
        mobilphoneL+=Temp[1]

        urlL=url(eachCompanyDir,fileList,eachDir)
        weichatL=weichat(eachCompanyDir,fileList,eachDir)
        companyNameL=companyName(eachCompanyDir,fileList,eachDir)

        # ------------------------------------------------
        otherRes=otherHandle(eachCompanyDir,fileList,eachDir)

        emailL+=otherRes['email']
        addressL+=otherRes['address']
        urlL+=otherRes['url']
        mobilphoneL+=otherRes['mobilphone']
        telphoneL+=otherRes['telphone']

        res={}
        res['qq']=set(qqL)
        res['email']=set(emailL)
        res['address']=set(addressL)
        res['mobilphone']=set(mobilphoneL)
        res['telphone']=set(telphoneL)
        res['url']=set(urlL)
        res['weichat']=set(weichatL)
        res['companyName']=set(companyNameL)
        result.append(res)
    return result

def showOne(target):
    if len(target)!=0:
        return target[0]
    else:
        return ""

def download2(request):
    logger.warning("qing qiu page......")
    wordMd5=request.GET.get('filenameMd5')

    tmps=buildCompanyInfoResult1(wordMd5)
    wb = xlwt.Workbook(encoding='utf-8')
    ws = wb.add_sheet('联系信息')
    ws.write(0, 0, 'QQ')
    ws.write(0, 1, '邮箱')
    ws.write(0, 2, '地址')
    ws.write(0, 3, '手机')
    ws.write(0, 4, '电话')
    ws.write(0, 5, '网址')
    ws.write(0, 6, '微信')
    ws.write(0, 7, '公司名')
    #
    excel_row = 1

    for tmp in tmps:
        try:
            ws.write(excel_row, 0, ";".join(tmp['qq']))
            ws.write(excel_row, 1, ";".join(tmp['email']))
            ws.write(excel_row, 2, ";".join(tmp['address']))
            ws.write(excel_row, 3, ";".join(tmp['mobilphone']))
            ws.write(excel_row, 4, ";".join(tmp['telphone']))
            ws.write(excel_row, 5, ";".join(tmp['url']))
            ws.write(excel_row, 6, ";".join(tmp['weichat']))
            ws.write(excel_row, 7, ";".join(tmp['companyName']))
            excel_row = excel_row + 1
        except:
            continue
    #
    response = HttpResponse(content_type='application/vnd.ms-excel')
    response['Content-Disposition'] = 'attachment; filename=result.xls'
    wb.save(response)
    return response

def downloadTempFile(request):
    # logger.warning("qing qiu page......")

    wordMd5=request.GET.get('filenameMd5')
    # ===================创建中间临时文件存储目录=====================
    makeDir(wordManyInfoDir+"\\"+wordMd5,"tempDir")
    fileObj=FileOperation(wordManyInfoDir+"\\"+wordMd5+"\\"+"tempDir","")

    name=fileObj.fileCreate("tempCompanyName")
    qq=fileObj.fileCreate("tempqq")
    email=fileObj.fileCreate("tempemail")
    address=fileObj.fileCreate("tempaddress")
    mobilphone=fileObj.fileCreate("tempmobilphone")
    telphone=fileObj.fileCreate("temptelphone")
    url=fileObj.fileCreate("tempurl")
    weichat=fileObj.fileCreate("tempweichat")

    targetDir=wordManyInfoDir+"\\"+wordMd5
    manyCompanyDir=os.listdir(targetDir)
    for eachDir in manyCompanyDir:
        eachCompanyDir=targetDir+"\\"+eachDir
        fileList=os.listdir(eachCompanyDir)

        otherRes=otherHandle(eachCompanyDir,fileList,eachDir)
        emailOther=otherRes['email']
        addressOther=otherRes['address']
        urlOther=otherRes['url']
        mobilphoneOther=otherRes['mobilphone']
        telphoneOther=otherRes['telphone']

        otherTempHandle(emailOther,email)
        otherTempHandle(addressOther,address)
        otherTempHandle(urlOther,url)
        otherTempHandle(mobilphoneOther,mobilphone)
        otherTempHandle(telphoneOther,telphone)

        readTxtContent(fileList,'qq',eachDir,qq,eachCompanyDir)
        readTxtContent(fileList,'weichat',eachDir,weichat,eachCompanyDir)
        readTxtContent(fileList,'email',eachDir,email,eachCompanyDir)
        readTxtContent(fileList,'address',eachDir,address,eachCompanyDir)
        readTxtContent(fileList,'phone',eachDir,mobilphone,eachCompanyDir)
        readTxtContent(fileList,'telphone',eachDir,telphone,eachCompanyDir)
        readTxtContent(fileList,'url',eachDir,url,eachCompanyDir)
        readTxtContent(fileList,'companyName',eachDir,name,eachCompanyDir)




    fileObj.fileClose(name)
    fileObj.fileClose(qq)
    fileObj.fileClose(email)
    fileObj.fileClose(address)
    fileObj.fileClose(mobilphone)
    fileObj.fileClose(telphone)
    fileObj.fileClose(url)
    fileObj.fileClose(weichat)
    # ============================结束==============================


    utilities = ZipUtilities()
    utilities.toZip(wordManyInfoDir+"\\"+wordMd5+"\\"+"tempDir",u"中间文件")

    response = StreamingHttpResponse(utilities.zip_file, content_type='application/zip')
    response['Content-Disposition'] = 'attachment; filename=temp.zip'
    return response



    '''
    temp = tempfile.TemporaryFile()
    archive = zipfile.ZipFile(temp, 'w', zipfile.ZIP_DEFLATED)
    #
    archive.write('F:\\target\\helloword.txt','helloword.txt')
    archive.write('F:\\target\\regioninfo.pkl','regioninfo.pkl')
    archive.close()

    # temp = tempfile.TemporaryFile()
    # print "================================"
    data=temp.tell()
    # print "================================"
    temp.seek(0)

    wrapper = FileWrapper(temp)
    response = HttpResponse(wrapper, content_type='application/zip')
    response['Content-Disposition'] = 'attachment; filename=test.zip'
    response['Content-Length'] = data
    return response
    '''

def multipleuploadhandle(request):
    if request.method == "POST":    # 请求方法为POST时，进行处理
        files =request.FILES.getlist('wordfile1')
        # print len(Files)
        if not files:
            return HttpResponse("请选择上传文件！")

        totalSize=0
        for f in files:
            totalSize+=f.size
        if totalSize > 1024*1024*maxSize:
            return HttpResponse("文件总量不能超过1M")

        multipleFile={}
        for f in files:
            # 获取文件名称和对应的md5
            name=f.name.split(".")
            fMd5=Word2Txt.getMd5(name[0]+str(time.time()).decode('utf-8'))

            fName=fMd5+u"."+name[1]
            multipleFile[fMd5]=fName

            destination = open(os.path.join(wordDir,fName),'wb+')    # 打开特定的文件进行二进制的写操作

            for chunk in f.chunks():      # 分块写入文件
                destination.write(chunk)
            destination.close()

        tmps=multipleBuildResult(multipleFile)

        # print multipleFile

        fileJson=multipleFile.keys()

        fileJson=json.dumps(fileJson)

        jieguo=[]

        for tmp in tmps:
            if len(tmp['qq'])!=0:
                tmp['qq']=tmp['qq'][0]
            else:
                tmp['qq']=""

            if len(tmp['email'])!=0:
                tmp['email']=tmp['email'][0]
            else:
                tmp['email']=""

            if len(tmp['address'])!=0:
                tmp['address']=str(tmp['address'][0])
            else:
                tmp['address']=""

            if len(tmp['mobilphone'])!=0:
                tmp['mobilphone']=tmp['mobilphone'][0]
            else:
                tmp['mobilphone']=""

            if len(tmp['telphone'])!=0:
                tmp['telphone']=tmp['telphone'][0]
            else:
                tmp['telphone']=""

            if len(tmp['url'])!=0:
                tmp['url']=tmp['url'][0]
            else:
                tmp['url']=""

            if len(tmp['weichat'])!=0:
                tmp['weichat']=tmp['weichat'][0]
            else:
                tmp['weichat']=""

            if len(tmp['companyName'])!=0:
                tmp['companyName']=tmp['companyName'][0]
            else:
                tmp['companyName']=""


            jieguo.append(tmp)

        # print jieguo
        return render_to_response('ceshi/result.html',{'jieguo1':jieguo,'fileJson':fileJson})

def multipleBuildResult(files):
    # files是一个字典
    w2t=Word2Txt()
    itemworkdir=itemdir

    result=[]
    for filemd5,name in files.items():

        w2t.word2FileSlice(wordDir+"\\",name,filemd5)

        fileList=os.listdir(itemworkdir)

        qqL=qq(itemworkdir,fileList,filemd5)
        emailL=email(itemworkdir,fileList,filemd5)
        addressL=address(itemworkdir,fileList,filemd5)
        mobilphoneL=mobilphone(itemworkdir,fileList,filemd5)
        telphoneL=[]
        Temp=telphone(itemworkdir,fileList,filemd5)  #Temp中的结果：telphoneres,mobilphoneres
        telphoneL+=Temp[0]
        mobilphoneL+=Temp[1]

        urlL=url(itemworkdir,fileList,filemd5)
        weichatL=weichat(itemworkdir,fileList,filemd5)
        companyNameL=companyName(itemworkdir,fileList,filemd5)
        # --------------------------------------------------------
        otherRes=otherHandle(itemdir,fileList,filemd5)
        emailL+=otherRes['email']
        addressL+=otherRes['address']
        urlL+=otherRes['url']
        mobilphoneL+=otherRes['mobilphone']
        telphoneL+=otherRes['telphone']

        res={}
        res['qq']=qqL
        res['email']=emailL
        res['address']=addressL
        res['mobilphone']=mobilphoneL
        res['telphone']=telphoneL
        res['url']=urlL
        res['weichat']=weichatL
        res['companyName']=companyNameL

        result.append(res)
    return result

def multipleBuildResult1(files):
    # files是一个字典
    # w2t=Word2Txt()
    itemworkdir=itemdir
    fileList=os.listdir(itemworkdir)
    result=[]
    for filemd5 in files:

        qqL=qq(itemworkdir,fileList,filemd5)
        emailL=email(itemworkdir,fileList,filemd5)
        addressL=address(itemworkdir,fileList,filemd5)
        mobilphoneL=mobilphone(itemworkdir,fileList,filemd5)
        telphoneL=[]
        Temp=telphone(itemworkdir,fileList,filemd5)
        telphoneL+=Temp[0]
        mobilphoneL+=Temp[1]

        urlL=url(itemworkdir,fileList,filemd5)
        weichatL=weichat(itemworkdir,fileList,filemd5)
        companyNameL=companyName(itemworkdir,fileList,filemd5)
        # --------------------------------------------------------
        otherRes=otherHandle(itemdir,fileList,filemd5)
        emailL+=otherRes['email']
        addressL+=otherRes['address']
        urlL+=otherRes['url']
        mobilphoneL+=otherRes['mobilphone']
        telphoneL+=otherRes['telphone']

        res={}
        res['qq']=set(qqL)
        res['email']=set(emailL)
        res['address']=set(addressL)
        res['mobilphone']=set(mobilphoneL)
        res['telphone']=set(telphoneL)
        res['url']=set(urlL)
        res['weichat']=set(weichatL)
        res['companyName']=set(companyNameL)

        result.append(res)
    return result

def download1(request):
    logger.warning("qing qiu page......")
    files=request.GET.get('para')
    multipleFile=json.loads(files)
    tmps=multipleBuildResult1(multipleFile)
    wb = xlwt.Workbook(encoding='utf-8')
    ws = wb.add_sheet('联系信息')
    ws.write(0, 0, 'QQ')
    ws.write(0, 1, '邮箱')
    ws.write(0, 2, '地址')
    ws.write(0, 3, '手机')
    ws.write(0, 4, '电话')
    ws.write(0, 5, '网址')
    ws.write(0, 6, '微信')
    ws.write(0, 7, '公司名')
    #
    excel_row = 1

    for tmp in tmps:
        try:
            ws.write(excel_row, 0, ";".join(tmp['qq']))
            ws.write(excel_row, 1, ";".join(tmp['email']))
            ws.write(excel_row, 2, ";".join(tmp['address']))
            ws.write(excel_row, 3, ";".join(tmp['mobilphone']))
            ws.write(excel_row, 4, ";".join(tmp['telphone']))
            ws.write(excel_row, 5, ";".join(tmp['url']))
            ws.write(excel_row, 6, ";".join(tmp['weichat']))
            ws.write(excel_row, 7, ";".join(tmp['companyName']))
            excel_row = excel_row + 1
        except:
            continue
    #
    response = HttpResponse(content_type='application/vnd.ms-excel')
    response['Content-Disposition'] = 'attachment; filename=result.xls'
    wb.save(response)
    return response


def downloadMultiTempFile(request):
    files=request.GET.get('para')

    targetdirMd5=Word2Txt.getMd5(files+str(time.time()).decode('utf-8'))
    # fileDir=itemdir
    makeDir(itemdir,targetdirMd5)

    multipleFile=json.loads(files)
    fileList=os.listdir(itemdir)

    fileObj=FileOperation(itemdir+"\\"+targetdirMd5,"")

    name=fileObj.fileCreate("tempCompanyName")
    qq=fileObj.fileCreate("tempqq")
    email=fileObj.fileCreate("tempemail")
    address=fileObj.fileCreate("tempaddress")
    mobilphone=fileObj.fileCreate("tempmobilphone")
    telphone=fileObj.fileCreate("temptelphone")
    url=fileObj.fileCreate("tempurl")
    weichat=fileObj.fileCreate("tempweichat")


    for filemd5 in multipleFile:
        otherRes=otherHandle(itemdir,fileList,filemd5)
        emailOther=otherRes['email']
        addressOther=otherRes['address']
        urlOther=otherRes['url']
        mobilphoneOther=otherRes['mobilphone']
        telphoneOther=otherRes['telphone']

        otherTempHandle(emailOther,email)
        otherTempHandle(addressOther,address)
        otherTempHandle(urlOther,url)
        otherTempHandle(mobilphoneOther,mobilphone)
        otherTempHandle(telphoneOther,telphone)

        readTxtContent(fileList,'qq',filemd5,qq,itemdir)
        readTxtContent(fileList,'weichat',filemd5,weichat,itemdir)
        readTxtContent(fileList,'email',filemd5,email,itemdir)
        readTxtContent(fileList,'address',filemd5,address,itemdir)
        readTxtContent(fileList,'phone',filemd5,mobilphone,itemdir)
        readTxtContent(fileList,'telphone',filemd5,telphone,itemdir)
        readTxtContent(fileList,'url',filemd5,url,itemdir)
        readTxtContent(fileList,'companyName',filemd5,name,itemdir)

    fileObj.fileClose(name)
    fileObj.fileClose(qq)
    fileObj.fileClose(email)
    fileObj.fileClose(address)
    fileObj.fileClose(mobilphone)
    fileObj.fileClose(telphone)
    fileObj.fileClose(url)
    fileObj.fileClose(weichat)
    # ============================结束==============================


    utilities = ZipUtilities()
    utilities.toZip(itemdir+"\\"+targetdirMd5,u"多文件中间文件")

    response = StreamingHttpResponse(utilities.zip_file, content_type='application/zip')
    response['Content-Disposition'] = 'attachment; filename=temp.zip'
    return response


def customuploadhandle(request):
    if request.method == "POST":    # 请求方法为POST时，进行处理
        catalogFile =request.FILES.get("customfile", None)    # 获取上传的文件，如果没有文件，则默认为None
        contentFile =request.FILES.get("contentfile", None)    # 获取上传的文件，如果没有文件，则默认为None

        if not catalogFile:
            return HttpResponse("请选择目录文件上传！")
        if not contentFile:
            return HttpResponse("请选择内容文件上传！")

        if contentFile.size > 1024*1024*maxSize:
            return HttpResponse("上传的文件大小不能超过1M")

        res=catalogFile.read().decode('gbk').split('\n')   #目录中每一个标志行
        catalog={}
        count=1
        for each in res:
            if len(each.strip())==0:
                continue
            else:
                temp=[]
                linshi=unicode(replacing(each.strip()))
                temp.append(handleChengdui(linshi,flagChengDui))
                temp.append(Word2Txt.getMd5(each.strip()).decode('utf-8'))
                catalog[str(count)]=temp
                count+=1

        # 对上传内容word文件进行处理
        name=contentFile.name.split(".")
        filenameMd5=Word2Txt.getMd5(name[0]+str(time.time()).decode('utf-8'))  #文件名md5
        filename=filenameMd5+u"."+name[1]   #文件名md5+后缀
        destination = open(os.path.join(wordDir,filename),'wb+')    # 打开特定的文件进行二进制的写操作
        for chunk in contentFile.chunks():      # 分块写入文件
            destination.write(chunk)
        destination.close()

        makeDir(customInfoDir,filenameMd5)   #一个word文件对应一个txt文档

        customsegmentInfo(filename,filenameMd5,catalog)

        catalogJson=json.dumps(catalog)

        #将提取识别信息
        tmp=customBuildInfoResult(filenameMd5)

        return render_to_response('ceshi/result.html',{'jieguo3':tmp,'filenameMd5':filenameMd5,'catalog':catalogJson})
        # return render_to_response('ceshi/result.html',{'jieguo3':tmp,'filenameMd5':filenameMd5,'catalog':catalogJson},RequestContext(request))
        # return render(request,'ceshi/result.html',{'jieguo3':tmp,'filenameMd5':filenameMd5,'catalog':catalogJson})

def customsegmentInfo(fileName,parentDirMd5,catalog):
    w2t=Word2Txt()
    # w2t.word2CompanyInfoSegment(wordDir+"\\",fileName,parentDirMd5)
    w2t.word2CustomInfoSegmen(wordDir+"\\",fileName,parentDirMd5,catalog)

def customBuildInfoResult(wordFileMd5):
    wordDir=customInfoDir
    targetDir=wordDir+"\\"+wordFileMd5
    manyCompanyDir=os.listdir(targetDir)

    result=[]
    for eachDir in manyCompanyDir:
        eachCompanyDir=targetDir+"\\"+eachDir
        fileList=os.listdir(eachCompanyDir)      #表示一个数据块中有多少个联系方式单元
        # if len(fileList)<5:        #每一个单元模块都会有fullInfo.txt，联系单元种类阈值设置为3
        #     continue
        qqL=qq(eachCompanyDir,fileList,eachDir)
        emailL=email(eachCompanyDir,fileList,eachDir)
        # 地址仅仅识别一个
        addressL=address(eachCompanyDir,fileList,eachDir)
        mobilphoneL=mobilphone(eachCompanyDir,fileList,eachDir)
        telphoneL=[]
        Temp=telphone(eachCompanyDir,fileList,eachDir)
        telphoneL+=Temp[0]
        mobilphoneL+=Temp[1]


        urlL=url(eachCompanyDir,fileList,eachDir)
        weichatL=weichat(eachCompanyDir,fileList,eachDir)
        companyNameL=companyName(eachCompanyDir,fileList,eachDir)
        # --------------------------------------------------------
        otherRes=otherHandle(eachCompanyDir,fileList,eachDir)

        emailL+=otherRes['email']
        addressL+=otherRes['address']
        urlL+=otherRes['url']
        mobilphoneL+=otherRes['mobilphone']
        telphoneL+=otherRes['telphone']

        res={}
        res['qq']=showOne(qqL)
        res['email']=showOne(emailL)
        res['address']=showOne(addressL)
        res['mobilphone']=showOne(mobilphoneL)
        res['telphone']=showOne(telphoneL)
        res['url']=showOne(urlL)
        res['weichat']=showOne(weichatL)
        res['companyName']=showOne(companyNameL)
        result.append(res)
    return result

def customBuildInfoResult1(wordFileMd5):
    wordDir=customInfoDir
    targetDir=wordDir+"\\"+wordFileMd5
    manyCompanyDir=os.listdir(targetDir)

    result=[]
    for eachDir in manyCompanyDir:
        eachCompanyDir=targetDir+"\\"+eachDir
        fileList=os.listdir(eachCompanyDir)      #表示一个数据块中有多少个联系方式单元
        # if len(fileList)<5:        #每一个单元模块都会有fullInfo.txt，联系单元种类阈值设置为3
        #     continue
        qqL=qq(eachCompanyDir,fileList,eachDir)
        emailL=email(eachCompanyDir,fileList,eachDir)
        # 地址仅仅识别一个
        addressL=address(eachCompanyDir,fileList,eachDir)
        mobilphoneL=mobilphone(eachCompanyDir,fileList,eachDir)
        telphoneL=[]
        Temp=telphone(eachCompanyDir,fileList,eachDir)
        telphoneL+=Temp[0]
        mobilphoneL+=Temp[1]


        urlL=url(eachCompanyDir,fileList,eachDir)
        weichatL=weichat(eachCompanyDir,fileList,eachDir)
        companyNameL=companyName(eachCompanyDir,fileList,eachDir)
        # --------------------------------------------------------
        otherRes=otherHandle(eachCompanyDir,fileList,eachDir)

        emailL+=otherRes['email']
        addressL+=otherRes['address']
        urlL+=otherRes['url']
        mobilphoneL+=otherRes['mobilphone']
        telphoneL+=otherRes['telphone']

        res={}
        res['qq']=set(qqL)
        res['email']=set(emailL)
        res['address']=set(addressL)
        res['mobilphone']=set(mobilphoneL)
        res['telphone']=set(telphoneL)
        res['url']=set(urlL)
        res['weichat']=set(weichatL)
        res['companyName']=set(companyNameL)
        result.append(res)
    return result

def download3(request):
    logger.warning("qing qiu page......")
    wordMd5=request.GET.get('filenameMd5')

    tmps=customBuildInfoResult1(wordMd5)
    wb = xlwt.Workbook(encoding='utf-8')
    ws = wb.add_sheet('联系信息')
    ws.write(0, 0, 'QQ')
    ws.write(0, 1, '邮箱')
    ws.write(0, 2, '地址')
    ws.write(0, 3, '手机')
    ws.write(0, 4, '电话')
    ws.write(0, 5, '网址')
    ws.write(0, 6, '微信')
    ws.write(0, 7, '公司名')
    #
    excel_row = 1

    for tmp in tmps:
        try:
            ws.write(excel_row, 0, ";".join(tmp['qq']))
            ws.write(excel_row, 1, ";".join(tmp['email']))
            ws.write(excel_row, 2, ";".join(tmp['address']))
            ws.write(excel_row, 3, ";".join(tmp['mobilphone']))
            ws.write(excel_row, 4, ";".join(tmp['telphone']))
            ws.write(excel_row, 5, ";".join(tmp['url']))
            ws.write(excel_row, 6, ";".join(tmp['weichat']))
            ws.write(excel_row, 7, ";".join(tmp['companyName']))
            excel_row = excel_row + 1
        except:
            continue
    #
    response = HttpResponse(content_type='application/vnd.ms-excel')
    response['Content-Disposition'] = 'attachment; filename=result.xls'
    wb.save(response)
    return response

def downloadCustomTempFile(request):
    parentMd5=request.GET.get('filenameMd5')
    targetDir=customInfoDir+"\\"+parentMd5
    manyCompanyDir=os.listdir(targetDir)  #多个分块文件信息

    temp = tempfile.TemporaryFile()
    archive = zipfile.ZipFile(temp, 'w', zipfile.ZIP_DEFLATED)
    #
    for each in manyCompanyDir:
        archive.write(customInfoDir+"\\"+parentMd5+"\\"+each+"\\"+"fullInfo.txt",each+'.txt')
    # archive.write('F:\\target\\regioninfo.pkl','regioninfo.pkl')
    archive.close()
    data=temp.tell()
    # print "================================"
    temp.seek(0)

    wrapper = FileWrapper(temp)
    response = HttpResponse(wrapper, content_type='application/zip')
    response['Content-Disposition'] = 'attachment; filename=test.zip'
    response['Content-Length'] = data
    return response

# def temp(request):
#     return render_to_response('share/footer.html')
def downloadCustomFenPianFile(request):
    parentMd5=request.GET.get('filenameMd5')
    catalogJson=request.GET.get('catalog')
    catalog=json.loads(catalogJson)


    sourceDir=customInfoDir+"\\"+parentMd5
    #进行复制操作
    targetDir=customInfoDirTemp+"\\"+parentMd5
    shutil.copytree(sourceDir,targetDir,ignore=shutil.ignore_patterns('fullInfo.txt'))
    # 对文件夹进行重命名
    # [0]表示名字,[1]表示md5
    for value in catalog.values():
        os.rename(os.path.join(targetDir,value[1]),os.path.join(targetDir,value[0]))

    utilities = ZipUtilities()
    utilities.toZip(targetDir,u"自定义格式分片文件")
    response = StreamingHttpResponse(utilities.zip_file, content_type='application/zip')
    response['Content-Disposition'] = 'attachment; filename=temp.zip'
    # 删除临时分片文件夹
    # shutil.rmtree(targetDir)
    return response
